package com.sptpc.demo.controller;


import com.sptpc.demo.controller.requests.AddOrderRequest;
import com.sptpc.demo.controller.requests.ModifyDrinkRequest;
import com.sptpc.demo.controller.requests.ModifyOrderRequest;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.model.Order;
import com.sptpc.demo.service.IDrinkService;
import com.sptpc.demo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IDrinkService drinkService;
    @Autowired
    private IOrderService orderService;


//    新增订单
    @PostMapping("/")
    public Order addNewOrder(@RequestBody AddOrderRequest request) {
        log.info("订单信息：{}",request);
//        List<Order> items = drinkService.getDrinkByIds(request.getItems());
        Order order = new Order();
        BeanUtils.copyProperties(request,order);
        orderService.addOrder(order,request.getItems());
//        newOrder.setCustomer(request.getCustomer());
//        newOrder.setItems(items);
//        newOrder.setState(0);
//        newOrder = orderService.addNewOrder(newOrder);
        return order;
    }

 //删除订单
    @DeleteMapping("/{id}")
    public String deleteOrder(@PathVariable Integer id){
        log.info("要删除的订单id: {}",id);
        orderService.deleteOrder(id);
        return "删除成功";
    }


   //  get /order/脏三
    @GetMapping("/{customer}")
    public List<Order> getOrdersByCustomer(@PathVariable String customer){
        return orderService.getOrdersByCustomer(customer);
    }

    //修改订单
    @PutMapping("/")
    public Order modifyOrder(@RequestBody ModifyOrderRequest request){
        log.info("要修改的饮品: {}", request);
        //后期使用数据库update
        //1.通过查询id出饮品
       Order order= orderService.getOrderById(request.getId());
       order.setState(request.getState());
        // 3.更新数据库
        orderService.updateOrder(order);
        return order;
    }
}
